﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Configuration;
using System.Text;
using System.Windows.Forms;

namespace TestHarness_GUI
{
    public partial class TestHarness : Form
    {
        string logDestDir = string.Empty;
        public TestHarness()
        {
            InitializeComponent();
            string infoText = lblLogDestInfo.Text;
            txtUrl.Text = "http://localhost/openday";
            txtFileName.Text = @"F:\MS_Open_Day_Materials\DataFile";
            infoText = infoText.Replace("%path%", Properties.Settings.Default.TEST_LOG_DIRECTORY);

            lblLogDestInfo.Text = infoText;
            lblGenInfoText.Text = infoText;
            cboBrowserType.SelectedIndex = 0;
        }

        private void btnFile_Click(object sender, EventArgs e)
        {
            DialogResult dialogResult = folderBrowserDlgData.ShowDialog();//openFileDlg.ShowDialog();

            if (dialogResult == DialogResult.OK)
            {
                txtFileName.Text = folderBrowserDlgData.SelectedPath;//openFileDlg.FileName;
            }
        }

        private void btnLogDest_Click(object sender, EventArgs e)
        {
            DialogResult dialogResult = folderBrowseDlgLogDest.ShowDialog();

            if (dialogResult == DialogResult.OK)
            {
                txtLogDest.Text = folderBrowseDlgLogDest.SelectedPath;
            }
        }

        private void btnExecute_Click(object sender, EventArgs e)
        {
            dgvLog.Rows.Clear();

            
            if (txtUrl.Text.Length == 0)
            {
                MessageBox.Show("Please enter URL to test", "Test Runner", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtUrl.Focus();
            }

            if (txtFileName.Text.Length == 0)
            {
                MessageBox.Show("Please select test data file", "Test Runner", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtFileName.Focus();
            }
            else
            {

                TestRunner.dgv = dgvLog;
                TestRunner.treeLogFiles = treeViewLog;
                logDestDir = Properties.Settings.Default.TEST_LOG_DIRECTORY;
                if (txtLogDest.Text.Length > 0)
                    logDestDir = txtLogDest.Text;
                logDestDir = logDestDir + DateTime.Now.ToString("MM_dd_yyyy_hh_mm")+"\\";
                TestRunner.Execute(txtFileName.Text, logDestDir, cboBrowserType.SelectedItem.ToString(), txtUrl.Text.Trim());                

            }
        }

        private void tabControlHarness_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControlHarness.SelectedTab.Name == "tabPageLog")
            {
                if (!System.IO.Directory.Exists(logDestDir))
                {
                    MessageBox.Show("Please execute test before view log.", "Test Runner", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    tabControlHarness.SelectedTab = tabControlHarness.TabPages[1];
    
                }
            }
            
        }

        private void dgvLog_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
           /* if (e.ColumnIndex == 1)
            {
                ((TextAndImageColumn)this.dgvLog.Columns[1]).Image = Properties.Resources.loading;
            }*/
        }

        private void treeViewLog_AfterSelect(object sender, TreeViewEventArgs e)
        {
            webBrowserLog.Url = new Uri(@"file://" + logDestDir + e.Node.Text);
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            dgvCodeGeneration.Rows.Clear();


            if (txtFileName.Text.Length == 0)
            {
                MessageBox.Show("Please select test data file", "Test Runner", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtFileName.Focus();
            }
            else
            {

                TestGenerator.dgv = dgvCodeGeneration;
                TestGenerator.treeLogFiles = treeViewLog;
                logDestDir = Properties.Settings.Default.TEST_LOG_DIRECTORY;
                if (txtLogDest.Text.Length > 0)
                    logDestDir = txtLogDest.Text;
                logDestDir = logDestDir + DateTime.Now.ToString("MM_dd_yyyy_hh_mm") + "\\";
                TestGenerator.Generate(txtFileName.Text, logDestDir, txtUrl.Text.Trim());

            }

        }

        private void btnDataFile_Click(object sender, EventArgs e)
        {
            DialogResult dialogResult = folderBrowserDlgData.ShowDialog();//openFileDlg.ShowDialog();

            if (dialogResult == DialogResult.OK)
            {
                txtDataFile.Text = folderBrowserDlgData.SelectedPath;//openFileDlg.FileName;
            }
        }

        private void btnGeneratePath_Click(object sender, EventArgs e)
        {
            DialogResult dialogResult = folderBrowseDlgLogDest.ShowDialog();

            if (dialogResult == DialogResult.OK)
            {
                txtGeneratePath.Text = folderBrowseDlgLogDest.SelectedPath;
            }
        }


    }
}
